package com.qsd.doc4jsimpple.style;

import cn.hutool.core.collection.CollectionUtil;
import com.qsd.doc4jsimpple.constants.TbMergeEnum;
import com.qsd.doc4jsimpple.item.MarkStyle;
import com.qsd.doc4jsimpple.util.CollectionTool;
import lombok.Builder;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.docx4j.wml.JcEnumeration;
import org.docx4j.wml.STVerticalJc;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;


@Data
@Builder
public class DocxStyle {

    public static List<String> styleTypeList = Stream.of(
            "bold",
            "italic","underline",
            "fontSize","fontColor",
            "fontFamily").collect(Collectors.toList());;

    // =============== 段落

    /**
     * 对齐
     */
    private JcEnumeration align;

    // =============== 表格

    /**
     * 外边框 上右下左
     */
    private List<Long> margins;

    /**
     * 单元格颜色
     */
    private String cellColor;

    /**
     * 单元格宽度，默认1000，表格会自动按比例自适应
     */
    private Long cellWidth;

    /**
     * 垂直对齐
     */
    private STVerticalJc vAlign;

    /**
     * 垂直合并
     */
    private TbMergeEnum vMerge;

    /**
     * 水平合并
     */
    private TbMergeEnum hMerge;

    // =============== 文本

    /**
     * 粗体
     */
    private Boolean bold;

    /**
     * 斜体
     */
    private Boolean italic;

    /**
     * 下划线
     */
    private Boolean underline;

    /**
     * 中划线
     */
    private Boolean strike;

    private String width;

    private String height;

    /**
     * 字体大小
     */
    private Long fontSize;

    /**
     * 字体颜色
     */
    private String fontColor;

    /**
     * 字体背景色
     */
    private String highLightColor;

    /**
     * 字体
     */
    private String fontFamily;

    /**
     * 合并
     *
     * @param style 风格
     */
    public void merge(DocxStyle style) {
        if (style == null) {
            return;
        }
        if (style.align != null) {
            this.align = style.align;
        }
        if (style.margins != null) {
            this.margins = style.margins;
        }
        if (style.cellColor != null) {
            this.cellColor = style.cellColor;
        }
        if (style.cellWidth != null) {
            this.cellWidth = style.cellWidth;
        }
        if (style.vAlign != null) {
            this.vAlign = style.vAlign;
        }
        if (style.vMerge != null) {
            this.vMerge = style.vMerge;
        }
        if (style.hMerge != null) {
            this.hMerge = style.hMerge;
        }
        if (style.bold != null) {
            this.bold = style.bold;
        }
        if (style.italic != null) {
            this.italic = style.italic;
        }
        if (style.underline != null) {
            this.underline = style.underline;
        }
        if (style.fontSize != null) {
            this.fontSize = style.fontSize;
        }
        if (style.fontColor != null) {
            this.fontColor = style.fontColor;
        }
        if (style.fontFamily != null) {
            this.fontFamily = style.fontFamily;
        }
        if (style.highLightColor != null) {
            this.highLightColor = style.highLightColor;
        }
    }

    public static DocxStyle getStyleByAttrs(Map<String, Object> attrs,List<MarkStyle> marks) {
        DocxStyle style = DocxStyle.builder().build();
        if(CollectionUtil.isEmpty(attrs) && CollectionUtil.isEmpty(marks)){
            return style;
        }
        if(CollectionTool.containStyle(attrs,marks,"bold")){
            style.setBold(true);
        }
        if(CollectionTool.containStyle(attrs,marks,"width")){
            String width = CollectionTool.getStyle(attrs,marks,"width").toString();
            style.setWidth(width);
        }
        if(CollectionTool.containStyle(attrs,marks,"height")){
            String height = CollectionTool.getStyle(attrs,marks,"height").toString();
            style.setHeight(height);
        }
        if(CollectionTool.containStyle(attrs,marks,"highlight")){

            style.setHighLightColor(CollectionTool.getStyleInMarkStyle(marks,"highlight","color").toString());
        }
        if(CollectionTool.containStyle(attrs,marks,"italic")){
            style.setItalic(true);
        }
        if(CollectionTool.containStyle(attrs,marks,"underline")){
            style.setUnderline(true);
        }
        if(CollectionTool.containStyle(attrs,marks,"fontSize")){
            String fontSize = CollectionTool.getStyle(attrs,marks,"fontSize").toString();
            if(StringUtils.isNotEmpty(fontSize)){
                if(fontSize.indexOf("px")>-1){
                    fontSize = fontSize.replace("px","");
                }
                style.setFontSize(Long.valueOf(fontSize));
            }

        }
        if(CollectionTool.containStyle(attrs,marks,"color")){
            String fontColor = CollectionTool.getStyleInMarkStyle(marks,"textStyle","color").toString();
            if(fontColor.indexOf("rgb")>-1){
                fontColor = rgbStrToHexStr(fontColor);
            }
            style.setFontColor(fontColor);
        }
        if(CollectionTool.containStyle(attrs,marks,"fontFamily")){
            style.setFontFamily(CollectionTool.getStyle(attrs,marks,"fontFamily").toString());
        }
        if(CollectionTool.containStyle(attrs,marks,"strike")){
            style.setStrike(true);
        }
        return style;
    }

    public static String rgbStrToHexStr(String rgbStr){
        rgbStr = rgbStr.replaceAll("rgb\\(","");
        rgbStr = rgbStr.replaceAll("\\)","");
        String[] rgbArr = rgbStr.split(",");
        int r = Integer.parseInt(rgbArr[0].trim());
        int g = Integer.parseInt(rgbArr[1].trim());
        int b = Integer.parseInt(rgbArr[2].trim());
        return rgbToHex(r,g,b);
    }

    public static String rgbToHex(int r, int g, int b) {
        // 将RGB分量转换为十六进制字符串，并确保是2位长度（不足前面补0）
        String hexR = String.format("%02X", r);
        String hexG = String.format("%02X", g);
        String hexB = String.format("%02X", b);
        return "#" + hexR + hexG + hexB;
    }

    public static void main(String[] args) {
       String test =  rgbStrToHexStr("rgb(66, 165, 245)");
        System.out.println(test);
    }
}
